// @ts-nocheck
import prompt from '@system.prompt';
import socket from '@ohos.net.socket';
import router from '@system.router';

export default {
    data: {
        myTcp: null,
        send_msg: true,
        bindIP: "",
        connectIP: "",
        //    192.168.1.3
        connectPageVisible: "visible",
        dataPageVisible: "hidden",
        receive_data: "",
        humidity: "32.2",
        p2: "0.15",
        P10: "0.01",
        PA: "0.01",
        temp: "28.5",
        isOpen: true,
        isAuto: false,
        setting: false,
        color: "black",
        colour: "black",
    },
    onInit() {

    },
    GetIP(IP) {
        this.connectIP = IP.value
    },
    socketNet() {
        let tcp = socket.constructTCPSocketInstance();
        this.$app.$def.g_tcp = this.myTcp = tcp;
        // 绑定本机IP地址和端口。
        let bindAddress = {
            address: this.bindIP,
            port: 8888, // 绑定端口
            family: 1
        };
        tcp.bind(bindAddress, err => {
            if (err) {
                console.log(bindAddress.address + ' bind fail');
                prompt.showToast({
                    message: bindAddress.address + " bind fail",
                })
                return;
            }
            console.log(bindAddress.address + ' bind success');
            prompt.showToast({
                message: "bind success",
            })
        });
        // 连接到服务端的IP地址和端口。
        let connectAddress = {
            address: this.connectIP,
            port: 8888, // 连接服务端端口，如8888
            family: 1
        };
        tcp.connect({
            address: connectAddress,
            timeout: 6000
        }, err => {
            if (err) {
                console.log(connectAddress.address + ' connect fail');
                prompt.showToast({
                    message: "connect fail",
                });
                return;
            }
            console.log(connectAddress.address + ' connect success');
            prompt.showToast({
                message: connectAddress.address + " connect success",
            });
        });
        //读取数据
        this.revMsg();

    },
    revMsg() {
        this.myTcp.on('message', value => {
            console.log("on message")
            let buffer = value.message
            let dataView = new DataView(buffer)
            let str = ""
            for (let i = 0;i < dataView.byteLength; ++i) {
                str += dataView.getUint8(i).toString()
                str += ' '
            }
            this.humidity = dataView.getUint8(14).toString() + '.' + dataView.getUint8(15).toString()
            this.PA = (dataView.getUint8(4) * 16 + dataView.getUint8(5)).toString()
            this.p2 = (dataView.getUint8(8) * 16 + dataView.getUint8(9)).toString()
            this.P10 = (dataView.getUint8(10) * 16 + dataView.getUint8(11)).toString()
            this.temp = dataView.getUint8(12).toString() + '.' + dataView.getUint8(13).toString()
            console.log("on connect received:" + str)
            this.receive_data = buffer;
            prompt.showToast({
                message: str,
                duration: 3000
            });
        })
        this.myTcp.on('connect', () => {
            console.log("on connect")
        });
        this.myTcp.on('close', () => {
            console.log("on close")
        });
    },
    sendMsg() {
        this.myTcp.send({
            data: this.send_msg
        }, err => {
            if (err) {
                console.log('send fail');
                return;
            }
            prompt.showToast({
                message: "send successful"
            })
        })
    },
    //隐藏连接页面，显示数据页面
    nextPage() {
        this.connectPageVisible = "hidden"
        this.dataPageVisible = "visible"
    },
    backPage() {
        this.dataPageVisible = "hidden"
        this.connectPageVisible = "visible"
    },
    onOpen() {
        this.send_msg = this.isOpen = !this.isOpen
        //向服务端发送打开或关闭传感器的消息
        this.sendMsg();
    },
    onAuto() {
        if (this.isOpen) {
            this.isAuto = !this.isAuto
        } else {
            this.isAuto = false
        }
    },
    select(e) {
        switch(e.value){
            case "exit":
                this.backPage()
                break
            case "view":
                prompt.showToast({
                    message:"查看已连接设备"
                })
                break
            case "setting":
                prompt.showToast({
                    message:"查看设置"
                })
                break
            default:
                prompt.showToast({
                    message:"错误!"
                })
                break
        }
    },
    showMenu() {
        this.$element("menu").show({
            x: 0, y: 200
        });
    },
    change() {
        router.push({
            uri: 'pages/second/second'
        })
    },
}
